package com.yizhubao.app.activitys;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.xw.repo.XEditText;
import com.yizhubao.app.MainActivity;
import com.yizhubao.app.R;
import com.yizhubao.app.base.BaseActivityToolBar;
import com.yizhubao.app.beans.WXLoginBean;
import com.yizhubao.app.customviews.VerificationCodeText;
import com.yizhubao.app.presenters.IdCardVaildPresenter;
import com.yizhubao.app.presenters.SendCodePresenter;
import com.yizhubao.app.views.IdCardVaildView;
import com.yizhubao.app.views.SendCodeView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import h.framework.pacy.utils.LogUtil;
import h.framework.pacy.utils.StringUtil;
import h.framework.pacy.utils.ToastUtil;

/**
 * 创建人: PACY
 * 创建时间: 6/6/21 10:25 AM
 * 邮箱: pengwwe457621023@qq.com
 * 类描述: 实名认证
 */
public class AuthActivity extends BaseActivityToolBar implements IdCardVaildView, SendCodeView {
    @BindView(R.id.getCode_vc)
    VerificationCodeText getCode_vc;

    @BindView(R.id.name_et)
    EditText name_et;
    @BindView(R.id.idCode_et)
    XEditText idCode_et;
    @BindView(R.id.phone_et)
    XEditText phone_et;
    @BindView(R.id.code_et)
    EditText code_et;

    IdCardVaildPresenter presenter;
    SendCodePresenter codePresenter;

    String recordId;

    private int jumpType=1;//1 开屏页过来跳转到首页  2卡片过来到详情 3个人中心直接finish
    @Override
    protected int setContentLayout() {
        return R.layout.activity_auth;
    }

    @Override
    protected void initView() {
        setTitleOnLeftIcon(R.drawable.icon_back,"");
        presenter=new IdCardVaildPresenter();
        presenter.attachView(this);
        codePresenter=new SendCodePresenter();
        codePresenter.attachView(this);
        idCode_et.setSeparator(" ");
        idCode_et.setPattern(new int[]{3,3,8,4});
        idCode_et.setDisableClear(true);

        phone_et.setSeparator(" ");
        phone_et.setPattern(new int[]{3,4,4});
        phone_et.setDisableClear(true);

    }

    @Override
    protected void initData() {
        jumpType=getIntent().getExtras().getInt("jumpType");
        if(jumpType==2){
            //获取记录id
            recordId=getIntent().getExtras().getString("recordId");
        }
    }

    /**
     * 获取验证码
     */
    private void getCodeRequest(){
        showLoading("请稍候");
        String phone=phone_et.getText().toString().replaceAll(" ","");
        if(StringUtil.isEmpty(phone)){
            ToastUtil.showShort("请输入手机号");
            return;
        }
        Map<String,Object> map=new HashMap<>();
        map.put("phone",phone);
        map.put("type","2");
        codePresenter.sendCodeRequest(map);
    }

    /**
     * 发送认证请求
     */
    private void sendRequest(){
        String name=name_et.getText().toString();
        String idCard=idCode_et.getText().toString().trim().replace(" ","");
        String phone=phone_et.getText().toString().replaceAll(" ","");
        String code=code_et.getText().toString();

        LogUtil.i("idCard="+idCard);
        LogUtil.i("phone="+phone);
        if(StringUtil.isEmpty(name)){
            ToastUtil.showShort("请输入姓名");
            return;
        }
        if(StringUtil.isEmpty(idCard)){
            ToastUtil.showShort("请输入身份证号");
            return;
        }
        if(StringUtil.isEmpty(phone)){
            ToastUtil.showShort("请输入手机号");
            return;
        }
        if(StringUtil.isEmpty(code)){
            ToastUtil.showShort("请输入验证码");
            return;
        }

        Map<String,Object> map=new HashMap<>();
        map.put("idNo",idCard);
        map.put("name",name);
        map.put("phone",phone);
        map.put("type","2");
        showLoading("认证中");
        presenter.validRequest(map);
    }

    @OnClick(value = {R.id.getCode_vc,R.id.commit_btn})
    public void click(View v){
        if(v.getId()==R.id.getCode_vc){
            getCode_vc.setLenght(10*6000);
            getCodeRequest();
        }else if(v.getId()==R.id.commit_btn){
            sendRequest();
        }
    }

    @Override
    public void onSuccess(WXLoginBean data) {
        ToastUtil.showShort("认证成功!");
        switch (jumpType){
            case 1:
                startActivity(AuthActivity.this, MainActivity.class);
                break;
            case 2:
                Bundle bundle=new Bundle();
                bundle.putString("recordId",recordId);
                startActivity(AuthActivity.this,RecordDetailActivity.class,bundle);
                break;
            case 3:
                break;
        }
        finish();
    }

    @Override
    public void onCodeSuccess(boolean send) {
        getCode_vc.start();
    }
}